﻿package  {
	
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Stage extends MovieClip {
		// attribute
		var background:Background;	// background picture	
		var chara:Chara;  // character bee
		var defaultX:int;  // default horizontal position in the stage
		var gameTimer:Timer;  // timer for this program
		var formatText:TextFormat;	// format text
		var text1:TextField;  // for text 1
		var text2:TextField;  // for text 2		
		var text3:TextField;  // for text 3		
		var text4:TextField;  // for text 4	
		
		var nama:String;  // user's name
		
		// Component
		var bubble1:Bubble1;   // for bubble 1
		var bubble2:Bubble2;   // for bubble 2
		var bubble3:Bubble3;   // for bubble 3
		var box1:Box1;   // for box 1
		
		// Menu		
		var ayoBelajar:AyoBelajar;	// movie clip menu ayo belajar
		var mariBermain:MariBermain;	// movie clip menu mari bermain
		var latihanYuk:LatihanYuk;	// movie clip menu latihan yuk
		
		var bangunDatar:BangunDatar;   // movie clip menu bangun datar
		var bilangan:Bilangan;	// movie clip menu Bilangan
		var operasi:Operasi;  // movie clip menu Operasi
		
		var state:int;  // what state this time
						// 0 = main menu
						// 1 = state ayo belajar
						// 2 = state mari bermain 
						// 3 = state latihan yuk
						// 4 = menu materi
						
		var	materiState:int;  // state for choose knowledge
						
		var waktu:int;	// time flow
		var materiCounter;int //  time for flow materi
		var changeFinished:Boolean;  // change finished state
		
		// tambahan ANA
		var counterx : int;
		var changeMove : Boolean;
		
		
		public function Stage() {
			// constructor codes
			init();  // Inisiasi all attribute
			initText();// Inisiasi text
			
		//  Add Event Listener				
		//  -----------------------------------------------------
		//  Main Menu
		//  -----------------------------------------------------
			ayoBelajar.addEventListener(MouseEvent.CLICK, Menu1);
			mariBermain.addEventListener(MouseEvent.CLICK, Menu2);
			latihanYuk.addEventListener(MouseEvent.CLICK, Menu3);
			
			ayoBelajar.addEventListener(MouseEvent.MOUSE_OVER, mouseOver1);
			ayoBelajar.addEventListener(MouseEvent.MOUSE_OUT, mouseOut1);
			mariBermain.addEventListener(MouseEvent.MOUSE_OVER, mouseOver2);
			mariBermain.addEventListener(MouseEvent.MOUSE_OUT, mouseOut2);
			latihanYuk.addEventListener(MouseEvent.MOUSE_OVER, mouseOver3);
			latihanYuk.addEventListener(MouseEvent.MOUSE_OUT, mouseOut3);	
			
		//  -----------------------------------------------------
		//  Menu Belajar
		//  -----------------------------------------------------			
			bangunDatar.addEventListener(MouseEvent.CLICK, Menu4);
			bilangan.addEventListener(MouseEvent.CLICK, Menu5);
			operasi.addEventListener(MouseEvent.CLICK, Menu6);
			
			bangunDatar.addEventListener(MouseEvent.MOUSE_OVER, mouseOver4);
			bangunDatar.addEventListener(MouseEvent.MOUSE_OUT, mouseOut4);
			bilangan.addEventListener(MouseEvent.MOUSE_OVER, mouseOver5);
			bilangan.addEventListener(MouseEvent.MOUSE_OUT, mouseOut5);
			operasi.addEventListener(MouseEvent.MOUSE_OVER, mouseOver6);
			operasi.addEventListener(MouseEvent.MOUSE_OUT, mouseOut6);	
			
		//  -----------------------------------------------------
		//  construction timer
		//  -----------------------------------------------------
			gameTimer = new Timer(50);
			gameTimer.addEventListener(TimerEvent.TIMER, stateGame);			
			gameTimer.start();
		}
		
		public function init() {
			state = 0;
			waktu = 0;
			materiState = 0;
			
			counterx = 0;
			changeMove = false;
			
			// is change Finished??
			changeFinished = true;
			
			// screen
			defaultX = 30;
			this.stage.displayState = StageDisplayState.FULL_SCREEN;
			
			// contructor Background
			background = new Background();
			addChild(background);
			background.x = (this.stage.width/2)-(background.width/2) + defaultX;
			
			// constructor chara
			chara = new Chara();
			addChild(chara);
			chara.x = defaultX + 800;
			chara.y = 500;
			
			// constructor buuble1
			bubble1 = new Bubble1();
			addChild(bubble1);
			bubble1.x = defaultX + 297.8;
			bubble1.y = 176.35;
			bubble1.visible = false;
			
			// constructor buuble2
			bubble2 = new Bubble2();
			addChild(bubble2);
			bubble2.x = defaultX + 600.65;
			bubble2.y = 8.75;
			
			// constructor buuble3
			bubble3 = new Bubble3();
			addChild(bubble3);
			bubble3.x = defaultX + 668.2;
			bubble3.y = 83.7;
			bubble3.visible = false;
			
			// constructor box1
			box1 = new Box1();
			addChild(box1);
			box1.x = defaultX + 63.75;
			box1.y = 83.7;
			box1.alpha = 0;
			
			// Main Menu						
			// constructor ayo belajar
			ayoBelajar = new AyoBelajar();
			addChild(ayoBelajar);
			ayoBelajar.x = defaultX + 92.75;
			ayoBelajar.y = 73.25;
			ayoBelajar.alpha = 0.7;
			
			// constructor mari Bermain
			mariBermain = new MariBermain();
			addChild(mariBermain);
			mariBermain.x = defaultX + 28.25;
			mariBermain.y = 312.85;
			mariBermain.alpha = 0.7;
			
			// constructor latihan yuk
			latihanYuk = new LatihanYuk();
			addChild(latihanYuk);
			latihanYuk.x = defaultX + 189.2;
			latihanYuk.y = 498.85;
			latihanYuk.alpha = 0.7;
			
			// constructor Bangun Datar
			bangunDatar = new BangunDatar();
			addChild(bangunDatar);
			bangunDatar.x = defaultX + 89.1
			bangunDatar.y = 119.9;
			bangunDatar.alpha = 0;
			bangunDatar.visible = false;
			
			// constructor Bilangan
			bilangan = new Bilangan();
			addChild(bilangan);
			bilangan.x = defaultX + 89.1;
			bilangan.y = 298.85;
			bilangan.alpha = 0;
			bilangan.visible = false;
			
			// constructor Operasi
			operasi = new Operasi();
			addChild(operasi);
			operasi.x = defaultX + 89.1;
			operasi.y = 483.5;
			operasi.alpha = 0;
			operasi.visible= false;
		}
		
		public function initText() {
		//  constructor all Text
			text1 = new TextField();
			text2 = new TextField();
			text3 = new TextField();
			text4 = new TextField();
		
		//  widht for aall text
			text1.width = 600;	
			text2.width = 600;	
			text3.width = 600;	
			text4.width = 600;	
		
		//  position for text 1
			text1.x = bubble2.x + 60;
			text1.y = bubble2.y + 80;
			
			formatText = new TextFormat();			
			setFormatText(1,"Hai teman-teman", 40, "First Grader", "0x947000")
			
		//  add to stage
			addChild(text1);
			addChild(text2);
			addChild(text3);
			addChild(text4);
		}
		
		public function setFormatText(numberKal:int, kalimat:String, size:int, font:String, color:String):void {	
		//  set format text for text
			formatText.size = size;
			formatText.font = font;
			formatText.color = color;
			
			if (numberKal == 1)	{
				text1.text = kalimat;				
				text1.setTextFormat(formatText);
			}
			
			else if (numberKal == 2)	{
				text2.text = kalimat;				
				text2.setTextFormat(formatText);
			}
			
			else if (numberKal == 3)	{
				text3.text = kalimat;				
				text3.setTextFormat(formatText);
			}
			
			else if (numberKal == 4)	{
				text4.text = kalimat;				
				text4.setTextFormat(formatText);
			}
		}
		
		public function stateGame(e:TimerEvent) {
			
		//  controller
			
		//  =====================================================
		//  FLOW
		//  =====================================================
		
		// ------------------------
			// bee fly
			counterx += 1;
			if (changeMove)
				chara.x += 1;
			else
				chara.x -= 1;
				
			if (counterx % 5 == 0) {
				if (counterx % 10 == 0)
					chara.y += 6;
				else
					chara.y -= 6;
			}
			
			if (counterx == 40)
			{
				counterx = 0;
				changeMove = !(changeMove);
			}
			// -------------------------
		
			if (waktu <= 20) {
				waktu += 1;			
			}
			
			else if (waktu == 21) {				
				text1.x = bubble2.x + 90;
				text1.y = bubble2.y + 80;
				setFormatText(1, "Ngapain ya??", 40, "First Grader", "0x947000");
				waktu = 99;
			}
				
			changeStateIn();
			changeStateOut();
			changeMateri();
			
		}
			
	//  =====================================================
	//  Main Menu FUNCTION
	//  =====================================================
		
		public function Menu1(e:Event) {
			state = 1;
			changeFinished = false;
		}
		
		public function Menu2(e:Event) {
			state = 2;
			changeFinished = false;
		}
		
		public function Menu3(e:Event) {
			state = 3;
			changeFinished = false;
		}
		
		public function mouseOver1(e:Event) {
			if (state == 0) {
				ayoBelajar.alpha = 1;
			}
		}
		
		public function mouseOut1(e:Event) {
			if (state == 0) {
				ayoBelajar.alpha = 0.7;
			}
		}
		
		public function mouseOver2(e:Event) {
			if (state == 0) {
				mariBermain.alpha = 1;
			}
		}
		
		public function mouseOut2(e:Event) {
			if (state == 0) {
				mariBermain.alpha = 0.7;
			}
		}
		
		public function mouseOver3(e:Event) {
			if (state == 0) { 
				latihanYuk.alpha = 1;
			}
		}
		
		public function mouseOut3(e:Event) {
			if (state == 0) {
				latihanYuk.alpha = 0.7;
			}
		}
		
		
	//  =====================================================
	//  Menu Belajar FUNCTION
	//  =====================================================
		
		public function Menu4(e:Event) {
			if (state == 1) {
				state = 0;
			}
			
			else if (state == 2) {
				state = 0;
			}
			changeFinished = false;
		}
		
		public function Menu5(e:Event) {
			if (state == 1) {	
				
				state = 44;
				materiState = 0;
				materiCounter = 0;
				
				//  fade out
				bubble1.visible = false;
				text2.visible = false;
				
				//  fade in
				bubble3.visible = true;
				text1.visible = true;	
				text1.x = bubble3.x + 110;
				text1.y = bubble3.y + 120;
				setFormatText(1, "Materi pertama", 40, "First Grader", "0x947000");
			}
			
			else if (state == 2) {
				state = 0;
			}
			changeFinished = false;
		}
		
		public function Menu6(e:Event) {
			if (state == 1) {
				state = 0;
			}
			
			else if (state == 2) {
				state = 0;
			}
			changeFinished = false;
		}
		
		public function mouseOver4(e:Event) {
			if (state == 1||state == 2) {
				bangunDatar.alpha = 1;				
				text2.x = bubble1.x + 90;
				text2.y = bubble1.y + 100;
				setFormatText(2,"Disini kita akan belajar \n bangun datar", 40, "First Grader", "0x947000");
			}
		}
		
		public function mouseOut4(e:Event) {
			if (state == 1||state == 2) {
				bangunDatar.alpha = 0.7;
			}
		}
		
		public function mouseOver5(e:Event) {
			if (state == 1||state == 2) {
				bilangan.alpha = 1;
				text2.x = bubble1.x + 80;
				text2.y = bubble1.y + 100;
				setFormatText(2,"Ayo, kita mengenal tentang \n bilangan", 40, "First Grader", "0x947000");
			}
		}
		
		public function mouseOut5(e:Event) {
			if (state == 1||state == 2) {
				bilangan.alpha = 0.7;
			}
		}
		
		public function mouseOver6(e:Event) {
			if (state == 1||state == 2) { 
				operasi.alpha = 1;
				text2.x = bubble1.x + 20;
				text2.y = bubble1.y + 100;
				setFormatText(2,"Tau cara operasi bilangan nggk? \n ayo belajar disini", 40, "First Grader", "0x947000");
			}
		}
		
		public function mouseOut6(e:Event) {
			if (state == 1||state == 2) {
				operasi.alpha = 0.7;
			}
		}
		
	//  =====================================================
	//  FLOW FUNCTION
	//  =====================================================
	
		public function changeStateOut() {
		// transition to other page		
			if (state == 1||state == 2||state == 3) {
				if (ayoBelajar.alpha >= 0) {
					ayoBelajar.alpha -= 0.10;				
				}
				else ayoBelajar.visible = false;
				
				if (mariBermain.alpha >= 0) {
					mariBermain.alpha -= 0.10;				
				}
				else mariBermain.visible = false;
				
				if (latihanYuk.alpha >= 0) {
					latihanYuk.alpha -= 0.10;				
				}
				else {
					latihanYuk.visible = false;				
					bubble2.visible = false;
					text1.visible = false;
				}
			}
			
			else if (state == 0||state == 44||state == 45||state == 46) {
				if (bangunDatar.alpha >= 0) {
					bangunDatar.alpha -= 0.10;				
				}
				else bangunDatar.visible = false;
				
				if (bilangan.alpha >= 0) {
					bilangan.alpha -= 0.10;				
				}
				else bilangan.visible = false;
				
				if (operasi.alpha >= 0) {
					operasi.alpha -= 0.10;				
				}
				else {
					operasi.visible = false;				
					bubble1.visible = false;
					text2.visible = false;
				}
			}
		}
		
		public function changeStateIn() {			
		// transition to the page
			if (state == 0) {
				
				ayoBelajar.visible = true;
				mariBermain.visible = true;
				latihanYuk.visible = true;
				
				if (ayoBelajar.alpha <= 0.7) {
					ayoBelajar.alpha += 0.10;				
				}
				if (mariBermain.alpha <= 0.7) {
					mariBermain.alpha += 0.10;				
				}
				if (latihanYuk.alpha <= 0.7) {
					latihanYuk.alpha += 0.10;				
				}	
				else {					
					if (!changeFinished) {
						bubble2.visible = true;
						text1.visible = true;	
						text1.x = bubble2.x + 70;
						text1.y = bubble2.y + 80;
						setFormatText(1, "Mau apa lagi ha ??!!", 40, "First Grader", "0x947000");
						changeFinished = true;
					}
				}
			}
			
			else if (state == 1||state == 2) {
				
				bangunDatar.visible = true;
				operasi.visible = true;
				bilangan.visible = true;
				
				if (bangunDatar.alpha <= 0.7){
					bangunDatar.alpha += 0.10;				
				}
				if (operasi.alpha <= 0.7) {
					operasi.alpha += 0.10;				
				}
				if (bilangan.alpha <= 0.7) {
					bilangan.alpha += 0.10;		
				}			
				else {
					if (!changeFinished) {
						bubble1.visible = true;
						text2.visible = true;	
						text2.x = bubble1.x + 90;
						text2.y = bubble1.y + 120;
						setFormatText(2, "Mau materi yang mana??", 40, "First Grader", "0x947000");
						changeFinished = true;
					}
				}
			}
			
			else if (state == 44) {
			//  fade in to bilangan
				if (box1.alpha <= 0.7){
					box1.alpha += 0.10;				
				}	
				else {
					if (!changeFinished) {
						changeFinished = true;
					}
				}
			}
		}
	
		public function changeMateri() {
			if (state == 44) {
				//  increase the materi counter
				materiCounter += 1;
				
				//  check for what materi
				if ( materiState == 1 || materiCounter == 30 ) {	
					text1.height = 600;
					text1.x = bubble3.x + 60;
					text1.y = bubble3.y + 120;
					setFormatText(1, "Ini adalah angka satu", 40, "First Grader", "0x947000");
					
					materiCounter = 30;
					materiState = 1;
				}
				
				else if ( materiState == 2 || materiCounter == 60 ) {	
					text1.height = 600;
					text1.x = bubble3.x + 60;
					text1.y = bubble3.y + 120;
					setFormatText(1, "Jika dieja", 40, "First Grader", "0x947000");
					
					materiCounter = 30;
					materiState = 1;
				}	
				
				else if ( materiState == 2 || materiCounter == 60 ) {	
					text1.height = 600;
					text1.x = bubble3.x + 60;
					text1.y = bubble3.y + 120;
					setFormatText(1, "Jika dieja", 40, "First Grader", "0x947000");
					
					materiCounter = 30;
					materiState = 1;
				}

			}
		}
		
	}
}
